package nobu.nakata.nfcfare.resource.mapper.strategy;

import net.sf.jsr107cache.Cache;
import nobu.nakata.nfcfare.resource.mapper.IPersistenceStrategy;



public class Caching<Resource, Key> implements IPersistenceStrategy<Resource, Key> {
	private Cache cache;
	private ICacheKeyFactory<Resource, Key> keyFactory;
	
	public Caching(Cache cache, ICacheKeyFactory<Resource, Key> keyFactory) {
		this.cache			= cache;
		this.keyFactory		= keyFactory;
	}

	@Override
	public Resource get(Key key) { 
		return (Resource) cache.get(keyFactory.makeCacheKey(key));
	}
	@Override
	public void set(Resource item) {
		cache.put(keyFactory.makeCacheKeyFromValue(item), item);
	}
	@Override
	public void remove(Resource item) {
		cache.remove(keyFactory.makeCacheKeyFromValue(item));
	}
}
